// 配置跨越携带cookie 和证书
import axios from 'axios'
import Cookies from 'js-cookie'

axios.defaults.withCredentials = true
axios.defaults.timeout = 50000
window._xvueAxios = axios

// 请求预处理
axios.interceptors.request.use(
  config => {
    config.headers['XSRF-TOKEN'] = Cookies.get('XSRF-TOKEN')
    config.headers['version-number'] = 'V1R1C00_1'

    if (location.href.indexOf('localhost') > -1) {
      // token
      config.headers['gc-authentication'] = 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxMWQ1ODU5OC1kYTgzLTRkZGYtOTBjNy04ZmE1NzQ1ZTQzNDMiLCJleHAiOjE1ODkwMTUyMjd9.4G7BqY_dNwU4BvuRQfr0m3DTuuV4faJA1OYAKo5T1JeJ54MBvqxwgFN7PKxFnIVGL21CFXYN-4S_Cbd-Z00iWA'
      if (config.url.indexOf('auth/uc/permissions') < 0) {
        config.headers['user-info'] = 'ewogICAgInVzZXJJZCI6ICJzdmMtMSIsCiAgICAidXNlcm5hbWUiOiAidWNfZnJtIiwKICAgICJlbmFibGVkIjogMSwKICAgICJ1c2VyVHlwZSI6IDAsCiAgICAiZW1haWwiOiBudWxsLAogICAgInBob25lIjogbnVsbCwKICAgICJvcmdJZCI6IG51bGwsCiAgICAidG9rZW4iOiBudWxsLAogICAgImtleXN0b25lVG9rZW4iOiBudWxsLAogICAgInRlbmFudElkIjogbnVsbCwKICAgICJ0ZW5hbnROYW1lIjogbnVsbCwKICAgICJ0YXJnZXRUZW5hbnRJZCI6IG51bGwsCiAgICAidGFyZ2V0VGVuYW50TmFtZSI6IG51bGwsCiAgICAidXNlckljb24iOiBudWxsLAogICAgInRlbmFudHMiOiBbXSwKICAgICJyb2xlcyI6IFtdLAogICAgInByZW1pc2VUZW5hbnRzIjogW10sCiAgICAic2Vzc2lvblRpbWUiOiAwLAogICAgImFsaWFzTmFtZSI6ICLmnLrlmajotKblj7ciLAogICAgInVzZXJTcmMiOiAic3ZjLmZybSIsCiAgICAiY3VycmVudFJvbGUiOiBudWxsLAogICAgImN1cnJlbnRSb2xlTmFtZSI6IG51bGwsCiAgICAidGVuYW50UGFnZSI6IG51bGwsCiAgICAidGVuYW50UGFnZVVybCI6IG51bGwsCiAgICAibWFuYWdlcmxvY2t0YWciOiBudWxsLAogICAgIm9wZXJhdG9ySWQiOiBudWxsLAogICAgImFjdGl2ZVN0YXJ0VGltZSI6IG51bGwsCiAgICAiYWN0aXZlRW5kVGltZSI6IG51bGwsCiAgICAiY3VycmVudENsaWVudElkIjogIjg2MTU2MWI2LWQ4OTctNDg0ZC04NzRkLWU4MzlkODI0ZTcwZSIsCiAgICAiYXV0aF9hcmVhIjogW10sCiAgICAiYXV0aG9yaXRpZXMiOiBudWxsCn0='
      }
    }

    config.headers['client-code'] = window._adoPremise.clientId

    config.headers['tenant-code'] = window._adoPremise.tenantId
    config.headers['tenantCode'] = window._adoPremise.tenantId
    config.headers['tenantId'] = window._adoPremise.tenantId

    config.headers['uc-code'] = window._adoPremise.uc.instanceId

    if (config.url.indexOf('/data/v2/') > -1 || config.url.indexOf('/gridmanager/v1/') > -1) {
      if (process.env.NODE_ENV == 'development') {
        config.url = config.url.replace('/gridmanager/gridmanager/v1', '/gridmanager/v1')
      }
      return config
    }

    if (location.href.indexOf('localhost') < 0 && config.url.indexOf('/xvueServer') > -1) {
      config.url = config.url.replace('/xvueServer', '/xx/xvueapi')
      return config
    }

    // 判断是否登录或注销
    if (config.url.indexOf('/logout') > -1) {
      let cookeNames = Cookies.get()
      if (cookeNames) {
        for (let i = 0; i < Object.keys(cookeNames).length; i++) {
          Cookies.remove(cookeNames[i])
        }
      }
      Cookies.remove('XSRF-TOKEN')
      return config
    }

    return config
  },
  err => {
    return Promise.reject(err)
  }
)

axios.interceptors.response.use(
  response => {
    return response
  },
  error => {
    console.error(error)
    if (error.constructor.name == 'Cancel') {
      return error
    }
    if (error.response.status === 401) {
      window._xvueApp.$router.push({ name: 'tokenError', params: { siteId: window._adoPremise.uc.siteId } })
    }
    if (error.response.status === 403) {

    }
    return Promise.reject(error)
  }
)
